package com.leetcode.LC_MED;

public class LC543_4 {
    int max = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root != null) {
            dfs(root);
            dfs(root.left);
            dfs(root.right);
        }
        return max;
    }

    public int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = root.left == null ? 0 : dfs(root.left) + 1;
        int right = root.right == null ? 0 : dfs(root.right) + 1;
        max = Math.max(max, left + right);
        return Math.max(left, right);

    }
}
